package cn.findviewbydream.evil.app.service.impl;

import cn.findviewbydream.evil.app.service.AppService;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.io.File;

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.app.service.impl
 * 时间：17-2-6
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
@Service
public class AppControllerImpl implements AppService
{
    private static final String WAITER = "MOToWaiter";
    private static final String CHEF = "MOToChef";
    private static final String BOSS = "MOToBoss";

    private static Integer waiterCode = null;
    private static Integer chefCode = null;
    private static Integer bossCode = null;
    private static String waiterFileName;
    private static String chefFileName;
    private static String bossFileName;

    private static int times = 0;

    @NotNull
    @Override
    public String version(@NotNull String type, int code)
    {
        getAppCode();

        switch (type)
        {
            case WAITER:
                return code < waiterCode ? waiterFileName : "false";
            case CHEF:
                return code < chefCode ? chefFileName : "false";
            case BOSS:
                return code < bossCode ? bossFileName : "false";
            default:
                return "false";
        }
    }

    private void getAppCode()
    {
        if (waiterFileName != null && chefFileName != null && bossFileName != null)
            return;

        if (times >= 3)
            return;

        times++;

        String path = System.getProperty("root.path");
        path += "/app/";

        File root = new File(path);
        File[] files = root.listFiles();

        if (files != null)
        {
            int code;
            String name;
            for (File file : files)
            {
                name = file.getName();
                if (name.contains(WAITER))
                {
                    code = getCode(file);
                    if (waiterCode == null || code > waiterCode)
                    {
                        waiterCode = code;
                        waiterFileName = name;
                    }
                    continue;
                }

                if (name.contains(CHEF))
                {
                    code = getCode(file);
                    if (chefCode == null || code > chefCode)
                    {
                        chefCode = code;
                        chefFileName = name;
                    }

                    continue;
                }

                if (name.contains(BOSS))
                {
                    code = getCode(file);
                    if (bossCode == null || code > bossCode)
                    {
                        bossCode = code;
                        bossFileName = name;
                    }
                }
            }
        }
    }

    private int getCode(File file)
    {
        String name = file.getName();
        String code = name.substring(name.indexOf("code") + 4, name.indexOf(".apk"));
        return Integer.valueOf(code);
    }
}
